/*
 * @Author: 张小平
 * @Date:   2025-05-01
 * @Email:  qqshuqian@163.com
 * @Copyright © 2024 aseyun. All rights reserved.
 */
import {getDicList} from '@/framework-core/api/login'
import {getStore, setStore} from '@/framework-core/utils/store'

export function getDict(obj) {
  //obj.sex = ["nan","nv"];

  var dictemp = getStore({ name: 'jrdic' });
  if(dictemp){
    setDict(obj,dictemp);
    return;
  }
  getDicList().then((res) => {
    if(res.data.success){
       var data = res.data.data.data;
      setDict(obj,data);
      setStore({ name: 'jrdic', content: data })
    }
  })

}

export function setDict (obj,data) {
  var dmap = {};
  for(let n=0;n<data.length;n++){
    let dic = data[n];
    if(dic.parentId ==0){
      dmap[dic.id] = dic.code;
      obj[dic.code] = [];
    }
  }
  for(let n=0;n<data.length;n++){
    let dic = data[n];
    if(dic.parentId >0) {

      // if ((dic.code * 1 || dic.code * 1===0)) {
      //   dic.code = dic.code * 1;
      // }
      obj[dmap[dic.parentId]].push({"value": dic.code, "label": dic.name});
    }
  }
}

export function initOptionDict (columns) {
  if(!columns){
    return
  }
  debugger
  for(let n=0;n<columns.length;n++) {
    let column = columns[n];
    let dics = [];
    if (column.dicCode) {
      if (column.dicCodeType) {
        let datas = this.$dictObject[column.dicCode];
        if(!datas) {
          continue
        }
        for (let m = 0; m < datas.length; m++) {
          datas[m].value = datas[m].value * 1;
          dics.push(datas[m])
        }
      } else {
        dics = this.$dictObject[column.dicCode];
      }
      column.dicData = dics;
    }

  }
};

